unit editlearningformunit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
  ButtonPanel, database, LCLTranslator;

type

  { TEditLearningForm }

  TEditLearningForm = class(TForm)
    ResultButtonPanel: TButtonPanel;
    NoteEdit: TEdit;
    NoteLabel: TLabel;
    SkillComboBox: TComboBox;
    SkillLabel: TLabel;
    LevelLabel: TLabel;
    LevelSpinEdit: TSpinEdit;
    procedure CancelButtonClick(Sender: TObject);
    procedure LevelSpinEditChange(Sender: TObject);
    procedure NoteEditChange(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure SkillComboBoxChange(Sender: TObject);
    procedure FillItems;
  private
    ResultWasAccepted: Boolean;
    Db: TDatabase;
  public
    SelectedSkillId: Integer;
    SelectedLevel: Integer;
    SelectedNote: String;
    function ShowLearningEdit(ADb: TDatabase; ASkillLevel: Integer = 1; ASkillId: Integer = 1; ANote: String = ''): Boolean;
  end;

var
  EditLearningForm: TEditLearningForm;

implementation

{$R *.lfm}

{ TEditLearningForm }

procedure TEditLearningForm.SkillComboBoxChange(Sender: TObject);
begin
  SelectedSkillId := SkillComboBox.ItemIndex + 1;
end;

procedure TEditLearningForm.FillItems;
var
  SkillNames: TStringList;
begin
  if Db = nil then begin
    SkillComboBox.Items.Clear;
    Exit
  end;

  SkillNames := Db.GetNames(Db.Skills);
  try
    SkillComboBox.Items.Assign(SkillNames);
  finally
    SkillNames.Free;
  end;
end;

function TEditLearningForm.ShowLearningEdit(ADb: TDatabase;
  ASkillLevel: Integer = 1; ASkillId: Integer = 1; ANote: String = ''): Boolean;
begin
  ResultWasAccepted := False;
  Db := ADb;

  LevelSpinEdit.Value := ASkillLevel;
  SelectedLevel := ASkillLevel;

  FillItems;
  SkillComboBox.ItemIndex := ASkillId - 1;
  SelectedSkillId := ASkillId;

  NoteEdit.Text := ANote;
  SelectedNote := ANote;

  ShowModal;

  ShowLearningEdit := ResultWasAccepted
end;

procedure TEditLearningForm.OKButtonClick(Sender: TObject);
begin
  ResultWasAccepted := True;
  Close
end;

procedure TEditLearningForm.CancelButtonClick(Sender: TObject);
begin
  ResultWasAccepted := False;
  Close
end;

procedure TEditLearningForm.LevelSpinEditChange(Sender: TObject);
begin
  SelectedLevel := LevelSpinEdit.Value;
end;

procedure TEditLearningForm.NoteEditChange(Sender: TObject);
begin
  SelectedNote := NoteEdit.Text;
end;

end.

